package pacman;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Khanhoa
 */
public class Diemso {
    public static int HighScoreX=135;
    public static int HighScoreY=77;
    public static int HighScoreMaxChars=10;
    public static int ScoreX=345;
    public static int ScoreY=77;
    public static int ScoreMaxChars=10;


    public static int LoadHighScore(){
        HighScoreX = 135;
        HighScoreY = 77;
        HighScoreMaxChars=10;
        ScoreX = 345;
        ScoreY = HighScoreY;
        ScoreMaxChars = HighScoreMaxChars;
        int i=0; 
        try{
                File fh = new File("HighScores.txt");
                FileReader fr= new FileReader(fh);
                BufferedReader buf = new BufferedReader(fr);
                String tmpl = buf.readLine();
                buf.close();
                int comma=0;
                if (tmpl!=null){
                    comma = tmpl.indexOf("  ***  ");
                    String tmps = tmpl.substring(0,comma);
                    //System.out.println(tmps);
                    i= Integer.valueOf(tmps);
                }
        } catch (IOException e){}
        return i;
    }
 public static void NewHighScore(int Score,int HighScore){
        int n=0;
        String[] bufitem = new String[5];
        if (Score>HighScore&&"gameover".equals(Pacman.GameStatus)){    
            String Pname = JOptionPane.showInputDialog(null,"New High Score!!! \nEnter Your Name","High Score!!!",JOptionPane.PLAIN_MESSAGE);
            File fh = new File("HighScores.txt");
            try{
                FileReader fr= new FileReader(fh);
                BufferedReader buf = new BufferedReader(fr);
                boolean eof=false;
                while(!eof){
                    String tmpl = buf.readLine();
                    if(n<5){
                        bufitem[n]=tmpl;
                        n += 1;
                    }
                    if (tmpl==null){
                        eof=true;
                    }
                }
                buf.close();
                fr.close();
                fh.delete();
            } catch (IOException e){}
            try{
                FileWriter fw = new FileWriter(fh);
                fw.write(String.valueOf(Score) + "  ***  " + Pname + "\t\r\n");                
                for(n=0;n<4;n++){
                    if (bufitem[n]!=null){
                        fw.write(bufitem[n] + "\r\n");
                    }
                }
                fw.close();
                HighScore=Score;
            } catch (IOException e){}
        }
    }
     
    public static String GetScore(int sc,int maxc,Boolean addzeros){
        String tmp,zeros;
        int n;
        zeros="0";
        tmp = String.valueOf(sc);
        if (tmp.length()!=maxc){
            if (tmp.length()<maxc){
                // must add 0s
                if (addzeros==true) {
                    tmp = zeros.substring(0,(maxc-tmp.length())) + tmp;
                }
            } else {
                // cut down digits
                tmp = tmp.substring(0,maxc-4) + "...";
            }
        }
        return tmp;
    }
    public static void onDraw(Graphics g,int S,int H){
         Graphics2D comp2d=(Graphics2D)g;
         Font f=new Font("Arial",Font.BOLD,18);
         comp2d.setFont( f );
         comp2d.setColor(Color.yellow);
         comp2d.drawString(GetScore(H,HighScoreMaxChars,false),HighScoreX,HighScoreY);
         comp2d.setColor(Color.white);
         comp2d.drawString(GetScore(S,ScoreMaxChars,false),ScoreX,ScoreY);
    }
    
}
